- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33
Add Filtering Capabilities for Registered Abilities using collections (POC) #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            galatanovidiu
  wants to merge
  18
  commits into
  trunk
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
feature/filter-registered-abilities-v2
  
      
      
   
  
    
  
  
  
 
  
      
    base: trunk
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          Conversation
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
    Public constants are converted to private static properties Additionally, validation methods are updated to use guard clauses, and some boolean logic is simplified.
WP_Abilities_Query already returns all abilities when args is empty, making the backward compatibility check unnecessary.
Updates type hints to specify arrays of strings for 'category' and 'namespace' parameters.
Updates method names and related docblocks to clarify that query argument processing focuses on sanitization rather than validation.
Improves documentation to specify use of the query class for retrieving and filtering abilities.
Updates the `WP_Abilities_Collection::pluck()` method to support dot notation for accessing nested properties. This allows for more flexible data extraction, such as retrieving values from `meta` properties like `meta.show_in_rest`. Both the value and key parameters now support this syntax. The implementation is updated to handle nested data retrieval, and comprehensive unit tests and documentation are added to reflect this enhancement. Also corrects a heading number in the advanced filtering documentation.
Update documentation for advanced filtering and sorting to improve clarity and prevent common mistakes. - Clarify that the operator in the `where()` method is optional and defaults to an equality check. - Document that `all()` is an alias for `to_array()`. - Add a prominent note for `where_meta()` to specify that the 'meta.' prefix should be omitted from keys.
| Codecov Report❌ Patch coverage is  
 Additional details and impacted files@@             Coverage Diff              @@
##              trunk     #119      +/-   ##
============================================
- Coverage     86.65%   85.11%   -1.55%     
- Complexity      148      210      +62     
============================================
  Files            18       19       +1     
  Lines           982     1162     +180     
  Branches         92       90       -2     
============================================
+ Hits            851      989     +138     
- Misses          131      173      +42     
 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
 | 
      
        
      
      
  
    15 tasks
  
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
      
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Summary
Proof of Concept: Alternative implementation to #115 using a Collection-based approach for filtering and sorting abilities.
This PR introduces
WP_Abilities_Collection- a fluent, chainable collection class that provides Laravel-inspired filtering, sorting, and transformation methods for working with registered abilities. Instead of the query-based approach in #115, this implementation leverages in-memory collection operations for a more developer-friendly API.Key Differences from #115
#115 Approach: Uses
WP_Abilities_Queryclass with array-based filtering arguments (similar toWP_Query)This PR Approach: Uses
WP_Abilities_Collectionclass with fluent, chainable method calls (similar to Laravel Collections)Example Comparison
Query Approach (#115):
Collection Approach (This PR):
What's New
Core Collection Class
WP_Abilities_Collection- New collection class for filtering, sorting, and querying abilitiesIteratorAggregateandCountablefor native PHP iterationFiltering Methods
where($key, $value)orwhere($key, $operator, $value)- Generic property filtering with operators (=,!=,!==,>,<,>=,<=)where_in($key, $values)- Filter where property is in array of valueswhere_not_in($key, $values)- Filter where property is NOT in array of valueswhere_category($categories)- Filter by category (single string or array)where_namespace($namespaces)- Filter by namespace (single string or array)where_meta($filters)- Filter by metadata properties with dot notation supportfilter($callback)- Custom callback filteringsearch($term)- Full-text search across name, label, and descriptionSorting Methods
sort_by($property, $descending)- Sort by property name or custom callbacksort_by_desc($property)- Shorthand for descending sortreverse()- Reverse collection orderRetrieval & Utility Methods
all()/to_array()- Get all abilities as arrayfirst($callback, $default)- Get first ability (optionally with filter)last($callback, $default)- Get last ability (optionally with filter)get($name, $default)- Get ability by namepluck($value, $key)- Extract property values (supports dot notation:meta.annotations.readonly)keys()- Get all ability namesvalues()- Re-index collection with sequential keyscount()- Count abilities in collectionis_empty()/is_not_empty()- Check if collection is emptyChanges by Component
PHP Core
includes/abilities-api/class-wp-abilities-collection.php(577 lines)includes/abilities-api.php-wp_get_abilities()now returnsWP_Abilities_CollectionDocumentation
docs/8.advanced-filtering-and-sorting.md(441 lines) - Comprehensive guidedocs/4.using-abilities.md- Collection examplesJavaScript Client
Tests
tests/unit/abilities-api/wpAbilitiesCollection.phpDocumentation Highlights
The new
docs/8.advanced-filtering-and-sorting.mdincludes:Backward Compatibility
✅ Fully backward compatible
wp_get_abilities()without arguments still worksIteratorAggregate- can be used inforeachloopsto_array()method converts collection back to arrayPerformance Considerations
This is an in-memory collection approach, which means:
Discussion Points
Related